// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.database.parts;

import java.util.HashMap;
import java.util.List;

import org.dengues.core.components.NodePropertyParameter;
import org.dengues.core.warehouse.IWarehouseNode;
import org.dengues.designer.ui.database.editors.DBFileEditorInput;
import org.dengues.designer.ui.database.policies.DBDiagramLayoutEditPolicy;
import org.dengues.designer.ui.editors.GEFEditorFreeformRootEditPart;
import org.dengues.model.database.AbstractDBObject;
import org.dengues.model.database.DatabaseDiagram;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.RootEditPart;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.gef.editpolicies.RootComponentEditPolicy;
import org.eclipse.ui.IEditorInput;

public class DBDiagramEditPart extends AbstractDBGraphicalEditPart {

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#getModelChildren()
     */
    @Override
    protected List getModelChildren() {
        return ((DatabaseDiagram) getModel()).getDbChildren();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
     */
    @Override
    protected IFigure createFigure() {
        createNodeParameters();
        Figure f = new FreeformLayer();
        f.setBorder(new MarginBorder(3));
        f.setLayoutManager(new FreeformLayout());
        return f;
    }

    @Override
    protected void createEditPolicies() {
        installEditPolicy(EditPolicy.COMPONENT_ROLE, new RootComponentEditPolicy());
        installEditPolicy(EditPolicy.LAYOUT_ROLE, new DBDiagramLayoutEditPolicy());
    }

    public void notifyChanged(Notification notification) {
        int type = notification.getEventType();
        if (type == Notification.ADD || type == Notification.REMOVE) {
            refreshChildren();
        }
    }

    private IWarehouseNode node;

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#getAdapter(java.lang.Class)
     */
    @Override
    public Object getAdapter(Class key) {
        if (key == IWarehouseNode.class) {
            if (node == null) {
                RootEditPart rootEditPart = getRoot();
                if (rootEditPart instanceof GEFEditorFreeformRootEditPart) {
                    GEFEditorFreeformRootEditPart rootEditPart2 = (GEFEditorFreeformRootEditPart) rootEditPart;
                    IEditorInput editorInput = rootEditPart2.getEditorInput();
                    if (editorInput instanceof DBFileEditorInput) {
                        node = ((DBFileEditorInput) editorInput).getWarehouseNode();
                    }
                }
            }
            return node;
        }
        return super.getAdapter(key);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.database.parts.AbstractDBGraphicalEditPart#createPropertyParameters()
     */
    @Override
    protected void createPropertyParameters() {
        NodePropertyParameter param;
        HashMap properties = getCastModel().getProperties();
        param = new NodePropertyParameter(getCastModel());

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.database.parts.AbstractDBGraphicalEditPart#getCastModel()
     */
    @Override
    public AbstractDBObject getCastModel() {
        return ((DatabaseDiagram) getModel());
    }

    @Override
    protected void createMainParameters() {

    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getCommandStack".
     * 
     * @return
     */
    public CommandStack getCommandStack() {
        EditPart parent2 = getParent();
        if (parent2 instanceof GEFEditorFreeformRootEditPart) {
            return ((GEFEditorFreeformRootEditPart) parent2).getCommandStack();
        }
        return null;
    }
}
